1099C - Postcard - CodeForces Solution


constructive algorithms implementation *1200

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define pb push_back
#define yes cout << "YES" << endl
#define no cout << "NO" << endl
#define txtinput freopen("input.txt", "r", stdin)
#define txtoutput freopen("output.txt", "w", stdout)
#define fast ios::sync_with_stdio(NULL); cin.tie(0); cout.tie(0);
using namespace std;
void safwat()
{
  
   string s;
   cin>>s;
   int k;
   cin>>k;
   int count1=0,count2=0,total=0;
   for(int i=0;i<s.length();i++){
    if(s[i]=='?')count1++;
    if(s[i]=='*')count2++;
   }
   total=s.length()-count1-count2;
   if(k==s.length()&&count2==0&&count1==0){
       cout<<s;
   }else if (count2==0&&k>total){
       cout<<"Impossible";
   }else if(k<total&&count1+count2<total-k){
       cout<<"Impossible";
   }
   else if (k<=total){
       string a=""; 
       int x=total-k;
       for(int i=0;i<s.length();i++){
           if((s[i]=='*'||s[i]=='?')&&x>0){
               a.pop_back();               x--;
           }else if(s[i]!='*'&&s[i]!='?') {
               a+=s[i];
           }
       }
       cout<<a;
   }else if(k>total){
       string a=""; 
       int x=k-total;
       for(int i=0;i<s.length();i++){
           if(s[i]=='*'&&x>0){
               while(x){
                   a+=s[i-1];
                   x--;
               }
           }else  if(s[i]!='*'&&s[i]!='?'){
               a+=s[i];
           }
       }
       cout<<a;
   }
   

}
 
int32_t main()
{
    int t=1;
    //cin >> t;
    while (t--)
    {
        safwat();
       cout << endl;
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

760B - Frodo and pillows
1006A - Adjacent Replacements
1195C - Basketball Exercise
1206A - Choose Two Numbers
1438B - Valerii Against Everyone
822A - I'm bored with life
9A - Die Roll
1430B - Barrels
279B - Books
1374B - Multiply by 2 divide by 6
1093B - Letters Rearranging
1213C - Book Reading
1468C - Berpizza
1546B - AquaMoon and Stolen String
1353C - Board Moves
902A - Visiting a Friend
299B - Ksusha the Squirrel
1647D - Madoka and the Best School in Russia
1208A - XORinacci
1539B - Love Song
22B - Bargaining Table
1490B - Balanced Remainders
264A - Escape from Stones
1506A - Strange Table
456A - Laptops
855B - Marvolo Gaunt's Ring
1454A - Special Permutation
1359A - Berland Poker
459A - Pashmak and Garden
1327B - Princesses and Princes